﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class UIWT : ViewBase
    {    
        public VWTSAER TMV
        {
            get
            {
                return (VWTSAER)MV;
            }
        }

        #region ==FDZ===========
        public static readonly DependencyProperty FDZProperty = DependencyProperty.Register("FDZ",
        typeof(double), typeof(UIWT),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFDZChanged));
        public double FDZ
        {
            get
            {
                return (double)GetValue(FDZProperty);
            }
            set
            {
                SetValue(FDZProperty, value);
                OnFDZChg();
            }
        }
        private static void OnFDZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWT;
            if (src != null)
            {
                src.FDZ = (double)e.NewValue;
            }
        }
        public virtual void OnFDZChg()
        { }
        #endregion
        #region ==BANZFARB===========
        public static readonly DependencyProperty BANZFARBProperty = DependencyProperty.Register("BANZFARB",
            typeof(double), typeof(UIWT),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBANZFARBChanged));
        public double BANZFARB
        {
            get
            {
                return (double)GetValue(BANZFARBProperty);
            }
            set
            {
                SetValue(BANZFARBProperty, value);
                OnBANZFARBChg();
            }
        }
        private static void OnBANZFARBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWT;
            if (src != null)
            {
                src.BANZFARB = (double)e.NewValue;
            }
        }
       
        #endregion

        #region ==JWP===========
        public static readonly DependencyProperty JWPProperty = DependencyProperty.Register("JWP",
            typeof(string), typeof(UIWT),
            new FrameworkPropertyMetadata("J", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnJWPChanged));
        public string JWP
        {
            get
            {
                return (string)GetValue(JWPProperty);
            }
            set
            {
                SetValue(JWPProperty, value);
                OnJWPChg();
            }
        }
        private static void OnJWPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWT;
            if (src != null)
            {
                src.JWP = (string)e.NewValue;
            }
        }
        public virtual void OnJWPChg()
        { }
        #endregion


        #region ==IsJP===========
        public static readonly DependencyProperty IsJPProperty = DependencyProperty.Register("IsJP",
            typeof(bool), typeof(UIWT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsJPChanged));
        public bool IsJP
        {
            get
            {
                return (bool)GetValue(IsJPProperty);
            }
            set
            {
                SetValue(IsJPProperty, value);
                OnIsJPChg();
            }
        }
        private static void OnIsJPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWT;
            if (src != null)
            {
                src.IsJP = (bool)e.NewValue;
            }
        }
        public virtual void OnIsJPChg()
        { }
        #endregion

        #region ==CADPATTERN===========
        public static readonly DependencyProperty CADPATTERNProperty = DependencyProperty.Register("CADPATTERN",
            typeof(string), typeof(UIWT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCADPATTERNChanged));
        public string CADPATTERN
        {
            get
            {
                return (string)GetValue(CADPATTERNProperty);
            }
            set
            {
                SetValue(CADPATTERNProperty, value);
                OnCADPATTERNChg();
            }
        }
        private static void OnCADPATTERNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWT;
            if (src != null)
            {
                src.CADPATTERN = (string)e.NewValue;
            }
        }
        public virtual void OnCADPATTERNChg()
        { }
        #endregion

        #region ==REGULATOR===========
        public static readonly DependencyProperty REGULATORProperty = DependencyProperty.Register("REGULATOR",
            typeof(bool), typeof(UIWT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnREGULATORChanged));
        public bool REGULATOR
        {
            get
            {
                return (bool)GetValue(REGULATORProperty);
            }
            set
            {
                SetValue(REGULATORProperty, value);
                OnREGULATORChg();
            }
        }
        private static void OnREGULATORChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWT;
            if (src != null)
            {
                src.REGULATOR = (bool)e.NewValue;
            }
        }
        public virtual void OnREGULATORChg()
        { }
        #endregion
        public UIWT()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
         
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
                BANZFARB = 0.0;
                FDZ = 0.0;
                REGULATOR = false;
                IsJP = false;
            }
            else
            { 
                FDZ = TMV.WTBSRAPP;
                BANZFARB = TMV.WTBANZFARB;
                KEY = TMV.WTBSRFNR;
                if (TMV.WTBCADKEY2.Trim() == "J")
                    REGULATOR = true;
                else
                    REGULATOR = false;

                if (TMV.WTBCADKEY1.Trim() == "J")
                    IsJP = true;
                else
                    IsJP = false;

                
            }
        }
        public override void LOADBYAUF()
        { 
            TBMain.Clear();
            if (ORDERNUM.Length < 15)
                return;
            PM["SRFNR"] = ORDERNUM.Substring(0, 15); 
            SResult = TMV.LOADBYBS(PM);
            MergeData(SResult, true); 
            base.LOADBYAUF();
        } 
        public virtual void OnBANZFARBChg()
        {
            if (BANZFARB == 0)
            {
                return;
            }
            if (dwMaster == null)
                return;
            TMV.WTBANZFARB = BANZFARB;
            try
            {
                TBDetail.Clear();
                if (!TBDetail.Columns.Contains("Y0"))
                {
                    TBDetail.Columns.Clear();
                    TBDetail.Columns.Add("IDX", typeof(int));
                    TBDetail.Columns.Add("YARN", typeof(string));
                    TBDetail.Columns.Add("YENDS", typeof(int));
                    TBDetail.Columns.Add("YTTENDS", typeof(double));
                    TBDetail.Columns.Add("YRPT", typeof(string));
                    for (int idx = 0; idx < 698; idx++)
                    {

                        TBDetail.Columns.Add(new DataColumn("Y" + idx, typeof(string)));
                    }
                    TBDetail.AcceptChanges();
                }
                for (int idx = 0; idx < BANZFARB; idx++)
                {
                    dwData = TBDetail.Rows.Add();
                    dwData["IDX"] = idx;
                }
                for (int idx = 0; idx < 4; idx++)
                {
                    dwData = TBDetail.Rows.Add();
                    dwData["IDX"] = BANZFARB + idx;
                    dwData["YARN"] = "X";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
